package cn.pcManage.goodsManage.service.impl;

import cn.common.common.model.ServerResponse;
import cn.common.goods.model.BaseGoods;
import cn.common.goods.model.BaseGoodsStyle;
import cn.common.goods.model.BaseGoodsType;
import cn.pcManage.common.dao.ImageInfoMapper;
import cn.pcManage.common.service.IUtilService;
import cn.pcManage.goodsManage.dao.GoodsMapper;
import cn.pcManage.goodsManage.dao.GoodsStyleMapper;
import cn.pcManage.goodsManage.dao.GoodsTypeMapper;
import cn.pcManage.goodsManage.model.Goods;
import cn.pcManage.goodsManage.model.GoodsViews;
import cn.pcManage.goodsManage.service.IGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zhang on 2019/1/12.
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsTypeMapper goodsTypeMapper;

    @Autowired
    GoodsStyleMapper goodsStyleMapper;

    @Autowired
    IUtilService iUtilService;

    @Autowired
    ImageInfoMapper imageInfoMapper;

    @Override
    public ServerResponse<?> getGoodsList(BaseGoods goods) {
        Map<String,Object> resultMap=new HashMap<String, Object>(2);
        PageHelper.startPage(goods.getPage(),goods.getLimit());
        List<GoodsViews> goodsList = goodsMapper.getGoodsList(goods);
        Page<GoodsViews> goodsPage = (Page<GoodsViews>) goodsList;
        resultMap.put("total", goodsPage.getTotal());
        resultMap.put("infos", goodsList);
        return ServerResponse.createBySuccess(resultMap);
    }

    @Override
    public ServerResponse<?> addGoods(Goods goods) {
        BaseGoods baseGoods = goods.getBaseGoods();
        List<BaseGoodsType> goodsTypeList = goodsTypeMapper.getTypeListByFatherId(baseGoods.getTypeId());
        if (goodsTypeList!=null && goodsTypeList.size()>0){
            return ServerResponse.createByErrorMessage("请细化至子类目新增");
        }
        //商品新增
        goodsMapper.insertSelective(baseGoods);
        //更新主图
        iUtilService.saveImgAddress(baseGoods.getId(),goods.getMainImgPath(),"goods/main");
        //更新详情页
        iUtilService.saveImgAddress(baseGoods.getId(),goods.getDetailImgPath(),"goods/detail");
        List<BaseGoodsStyle> goodsStyles = goods.getGoodsStyles();
        for (BaseGoodsStyle goodsStyle:goodsStyles){
            goodsStyle.setGoodsId(baseGoods.getId());
            goodsStyleMapper.insertSelective(goodsStyle);
            imageInfoMapper.deleteByAddress(goodsStyle.getImgPath());
        }
        return ServerResponse.createBySuccessMessage("新增成功");
    }

    @Override
    public ServerResponse<?> updateGoods(BaseGoods goods) {
        goodsMapper.updateByPrimaryKeySelective(goods);
        return ServerResponse.createBySuccessMessage("更新成功");
    }

    @Override
    public ServerResponse<?> deleteGoods(Integer id) {
        BaseGoods goods = goodsMapper.selectByPrimaryKey(id);
        goods.setStatus("下架");
        goodsMapper.updateByPrimaryKeySelective(goods);
        return ServerResponse.createBySuccessMessage("下架成功");
    }

    @Override
    public ServerResponse<?> getGoodsInfo(Integer id) {
        Goods goods = new Goods();
        goods.setBaseGoods(goodsMapper.selectByPrimaryKey(id));
        goods.setTypeName(goodsTypeMapper.getTypeNameById(goods.getBaseGoods().getTypeId()));
        goods.setGoodsStyles(goodsStyleMapper.getListByGoodsId(id));
        return ServerResponse.createBySuccess(goods);
    }

    @Override
    public ServerResponse<?> updateGoodsStatus(BaseGoods goods) {
        BaseGoods goodsNew = new BaseGoods();
        goodsNew.setId(goods.getId());
        goodsNew.setStatus(goods.getStatus());
        goodsMapper.updateByPrimaryKeySelective(goodsNew);
        return ServerResponse.createBySuccessMessage("操作成功");
    }
}
